<?php defined( '_VALID_MOS' ) or die( include_once("../../404.php") );?>		
<div id="box_soluottruycap">
	<div id="sub_box_soluottruycap">
		<div class="bg_tt_right"><div class="bg_tt_left"><div class="bg_tt"><div class="text_tt">Lượt truy cập</div></div></div></div>
		<div id="sub_box_soluottruycap1">
			<div class="ct_right">
				<div class="ct_left">
					<div class="ct">
					
					<?php
					
						$modVisitCounterHelper = new modVisitCounterHelper;
						$modVisitCounterHelper->render();
						
						class modVisitCounterHelper
						{
							function render()
							{
								// Read our Parameters
								$today			=	'Hôm nay';
								$yesterday		=	'Hôm qua';
								$x_month		=	'Tháng này';
								$x_week			=	'Tuần này';
								$all			=	'Tất cả';
								$online			=	'Online';
								
								$locktime		=	60;
								$initialvalue	=	1;
								$records		=	1000000;
								
								$s_today		=	1;
								$s_yesterday	=	1;
								$s_all			=	1;
								$s_week			=	1;
								$s_month		=	1;
								$s_online		=	1;
								
								$s_digit		=	1;
								$disp_type 		= 	"blue";
								
								$widthtable		=	"100";
								$pretext  		= 	"";
								$posttext  		= 	"";
								
								// From minutes to seconds
								$locktime		=	$locktime * 60;
								
								// Check if table exists. When not, create it
								include("protected/dbconnect.php");	
						
								// Now we are checking if the ip was logged in the database. Depending of the value in minutes in the locktime variable.
								$day			 =	date('d');
								$month			 =	date('n');
								$year			 =	date('Y');
								$daystart		 =	mktime(0,0,0,$month,$day,$year);
								$monthstart		 =  mktime(0,0,0,$month,1,$year);
								
								// weekstart
								$weekday		 =	date('w');
								$weekday--;
								if ($weekday < 0)$weekday = 7;
								$weekday		 =	$weekday * 24*60*60;
								$weekstart		 =	$daystart - $weekday;
						
								$yesterdaystart	 =	$daystart - (24*60*60);
								$now			 =	time();
								$ip				 =	$_SERVER['REMOTE_ADDR'];
								
								$cm = $mysqli->prepare("SELECT MAX(id) as `all_visitors` FROM visitcounter");
								$cm->execute() or die($mysqli->error);
								$cm->bind_result($all_visitors);
								if($cm->fetch()){
									$all_visitors = $all_visitors;
								}
								$cm->close();
								
								if ($all_visitors == NULL) {
									$all_visitors = $initialvalue;
								} else {
									$all_visitors += $initialvalue;
								}
								
								// Delete old records
								$temp=$all_visitors-$records;
								
								if ($records>0){
								$mysqli->query("DELETE FROM visitcounter WHERE id<'$temp'");
									if ($mysqli->errno <> 0 ) {
										printf("Statement failed %d: (%s) %s\n", $mysqli->errno, $mysqli->sqlstate, $mysqli->error);
									}
								}

								$cm = $mysqli->prepare("SELECT COUNT(*) as `items` FROM visitcounter WHERE ip='$ip' AND (tm+'$locktime')>'$now'");
								$cm->execute() or die($mysqli->error);
								$cm->bind_result($items);
								if($cm->fetch()){
									$items = $items;
								}
								$cm->close();

								if (empty($items))
								{
									$mysqli->query("INSERT INTO visitcounter (id, tm, ip) VALUES ('', '$now', '$ip')");
									if ($mysqli->errno <> 0 ) {
										printf("Statement failed %d: (%s) %s\n", $mysqli->errno, $mysqli->sqlstate, $mysqli->error);
									}
								}
								
								$n				 = 	$all_visitors;
								$div = 100000;
								while ($n > $div) {
									$div *= 10;
								}
								
								$cm = $mysqli->prepare("SELECT COUNT(*) as `today_visitors` FROM visitcounter WHERE tm>'$daystart'");
								$cm->execute() or die($mysqli->error);
								$cm->bind_result($today_visitors);
								if($cm->fetch()){
									$today_visitors = $today_visitors;
								}
								$cm->close();
						
								$cm = $mysqli->prepare("SELECT COUNT(*) as `yesterday_visitors` FROM visitcounter WHERE tm>'$yesterdaystart' and tm<'$daystart'");		
								$cm->execute() or die($mysqli->error);
								$cm->bind_result($yesterday_visitors);
								if($cm->fetch()){
									$yesterday_visitors = $yesterday_visitors;
								}
								$cm->close();
								
								$cm = $mysqli->prepare("SELECT COUNT(*) as `week_visitors` FROM visitcounter WHERE tm>='$weekstart'");
								$cm->execute() or die($mysqli->error);
								$cm->bind_result($week_visitors);
								if($cm->fetch()){
									$week_visitors = $week_visitors;
								}
								$cm->close();
								
								$cm = $mysqli->prepare("SELECT COUNT(*) as `month_visitors` FROM visitcounter WHERE tm>='$monthstart'");
								$cm->execute() or die($mysqli->error);
								$cm->bind_result($month_visitors);
								if($cm->fetch()){
									$month_visitors = $month_visitors;
								}
								$cm->close();

								// Count Online in 20 minutes
								//$online_time	=	1*60;
								$cm = $mysqli->prepare("SELECT COUNT(*) as `online_visitors` FROM visitcounter WHERE tm>'$daystart' AND (tm+$locktime)>'$now'");
								$cm->execute() or die($mysqli->error);
								$cm->bind_result($online_visitors);
								if($cm->fetch()){
									$online_visitors = $online_visitors;
								}
								$cm->close();
						
								$content = '<div>';
								if ($pretext != "") $content .= '<div>'.$pretext.'</div>';
								// Show digit counter
								if($s_digit){		
									$content .= '<div style="text-align: center;">';
									while ($div >= 1) {
										$digit = $n / $div % 10;
										$content .= '<img src="block/block_soluottruycap/images/'.$disp_type.'/'.$digit.'.gif" alt="số người truy cập" />';
										$n -= $digit * $div;
										$div /= 10;
									}
									$content .= '</div>';
								}

								$content		 .=	'<div style="padding-right:10px;"><table cellpadding="0" cellspacing="0" style="text-align: center; width: '.$widthtable.'%;"><tbody align="center">';
								// Show today, yestoday, week, month, all statistic
								/*if($s_today)		$content		.=	modVisitCounterHelper::spaceer("vtoday.gif", $today, $today_visitors);
								if($s_yesterday)	$content		.=	modVisitCounterHelper::spaceer("vyesterday.gif", $yesterday, $yesterday_visitors);
								if($s_week)			$content		.=	modVisitCounterHelper::spaceer("vweek.gif", $x_week, $week_visitors);
								if($s_month)		$content		.=	modVisitCounterHelper::spaceer("vmonth.gif", $x_month, $month_visitors);*/
								if($s_online)		$content		.=	modVisitCounterHelper::spaceer("vall.gif", $online, $online_visitors);														
								
								$content		.= "</tbody></table></div>";
								if ($posttext != "") $content		.= '<div>'.$posttext.'</div>';
								$content .= "</div>";
								echo $content;
							}
							function spaceer($a1,$a2,$a3)
							{
								$ret  = '<tr style="height:20px;">';
								$ret .= '<td align="center"><img src="block/block_soluottruycap/images/'.$a1.'" alt="số người truy cập"/></td>';
								$ret .= '<td align="left">'.$a2.'</td>';
								$ret .= '<td align="right">'.$a3.'</td>';
								$ret .= '</tr>';
								return $ret;
							}
						}

						?>
															
						
					</div>
				</div>
			</div>
			<div class="bg_bt_right"><div class="bg_bt_left"><div class="bg_bt"><div class="text_bt"></div></div></div></div>
		</div>
	</div>
	<div class="clear"></div>
</div>